﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Trackr.Core.Entities;
using System.Reflection;

namespace Trackr.Core
{
	public class Trackr
	{
		private static object _lock = new object();
		private static Trackr _instance;
		private DataProvider.TrackrDataTable<TimeItem> _trackrDataTable;
		public static Trackr Instance
		{
			get
			{
				lock (_lock)
				{
					if (_instance == null)
						_instance = new Trackr();
					return _instance;
				}
			}
		}
		private Trackr()
		{
			_trackrDataTable = new Core.DataProvider.TrackrDataTable<Entities.TimeItem>();
		}
		public TimeItem GetTimeItem(int id)
		{
			return _trackrDataTable[id];
		}
		public TimeItem GetLatestTimeItem()
		{
			return _trackrDataTable.GetLatestEntry();
		}
		public IEnumerable<TimeItem> GetTimeItems(DateTime startDate, DateTime endDate)
		{
			return _trackrDataTable.Get().Where(i => i.StartDate >= startDate && i.StartDate <= endDate);
		}
		public int AddNew(TimeItem itemToSave)
		{
			return _trackrDataTable.Add(itemToSave);
		}
		public int AddNew(string txt)
		{
			var ti = new TimeItem(txt);
			return _trackrDataTable.Add(ti);
		}
		public void Update(TimeItem itemToUpdate, TimeItem itemData)
		{
			Update(itemToUpdate.Id, itemData);
		}
		public void Update(int itemId, TimeItem itemData)
		{
			_trackrDataTable.Update(itemId, itemData);
		}
		public void ClearData()
		{
			_trackrDataTable.Truncate();
		}
	}
}
